package 算法.leetcode.labuladong.数组和链表;

import 算法.leetcode.offer.base.ListNode;

/**
 * https://leetcode.cn/problems/intersection-of-two-linked-lists/
 * @author lchenglong
 * @date 2022/6/16
 */
public class 相交链表 {
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode p1 = headA ,p2 = headB;
            while (p1!=p2){
                if (p1!=null){
                    p1 = p1.next;
                } else {
                    // 首尾互换
                    p1 = headB;
                }
                if (p2!=null){
                    p2 = p2.next;
                }else {
                    // 首尾互换
                    p2 = headA;
                }
            }
            return p1;
        }
    }
}
